var config = require(path.resolve(process.env.npm_config_wm_root, './webpack/config'))
var webpack = config.webpack.it
var HtmlWebpackPlugin = config.webpack.tools.HtmlWebpackPlugin
var origWebpackBaseConfig = require('./webpack.base.conf')
var entries = Object.keys(origWebpackBaseConfig.entry)

// vendor it, never pack the useless resources.
module.exports = {
  plugins: [
    new HtmlWebpackPlugin({
      filename: 'index.html',
      template: path.resolve(config.base.appSrcRoot, './index.html'),
      excludeChunks: entries.filter(function (x) { return x !== 'app' }),
      inject: true,
      minify: {
        removeComments: true,
        collapseWhitespace: true,
        removeAttributeQuotes: true
        // more options:
        // https://github.com/kangax/html-minifier#options-quick-reference
      },
      // necessary to consistently work with multiple chunks via CommonsChunkPlugin
      chunksSortMode: 'dependency'
    }),
    // split vendor js into its own file
    new webpack.optimize.CommonsChunkPlugin({
      name: 'vendor',
      minChunks: function (module, count) {
        // any required modules inside node_modules are extracted to vendor
        return (
          module.resource &&
          /\.js$/.test(module.resource) &&
          // module.resource.indexOf( config.base.appNodeModulesPath ) === 0 ||
          (module.resource.indexOf( config.base.appContainerModulesPath ) === 0 ||
          module.resource.indexOf( config.base.wpsRootModulesPath ) === 0)
        )
      }
    }),
    // extract webpack runtime and module manifest to its own file in order to
    // prevent vendor hash from being updated whenever app bundle is updated
    new webpack.optimize.CommonsChunkPlugin({
      name: 'manifest',
      chunks: ['vendor']
    })
  ]
}
